// 一加载就执行 默认是单选
window.onload = function(){ 
	chrome.storage.local.get({status:'single'},function(items){
		var status = items.status ;
		switch(status){
			case "multiple":
				multiple();
				break;
			case "single":
				single();
				break;
			case "disabled":
				alert("已经禁用点击留痕功能,如要启用,请点击拓展插件图标进行选择!");
			default:
				console.log("当前的status是: " + status);
		}
	});
}

// 单选模式
function single(){
	loadTheLastClickRow();
	var s = document.getElementsByClassName("but-in");
	var l = s.length ; 
	for(i=0; i<l;i++){
		s[i].onclick=function(){
			rememberTheLastClickRow(this);
			for(j=0; j<l;j++){
				var ele = s[j];
				ele.style.backgroundColor = "";
			}
			this.style.backgroundColor = "green";
		}
	}
}

// 多选模式
function multiple(){
	loadTheLastClickRow();
	var s = document.getElementsByClassName("but-in");
	for(i=0; i<s.length;i++){
		s[i].onclick=function(){
			rememberTheLastClickRow(this);
			this.style.backgroundColor = "green";
		}
	}
}

// 记住上次选中的
function rememberTheLastClickRow(obj){
	chrome.storage.local.set({href:obj.getAttribute("href")},function(){});
}

// 加载上次选中
function loadTheLastClickRow(){
	chrome.storage.local.get({href:'null'},function(items){
		var clickHref = items.href ;
		if (clickHref == "null"){
			return ;
		}
		var s = document.getElementsByClassName("but-in");
		for(i=0; i<s.length;i++){
			var btn = s[i];
			var href = btn.getAttribute("href");
			if (href == clickHref){
				btn.style.backgroundColor = "green";
				break;
			}
		}
	});
}

